package cn.sccl.dirms.monitor.service.impl;

import cn.sccl.common.database.DataBase;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.ImageExtend;
import cn.sccl.dirms.monitor.model.SysImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author _minus
 * @create 2017-06-05 11:55
 */
@Service
public class ImageManagerImpl implements cn.sccl.dirms.monitor.service.ImageManager {



    @Autowired
    private DataBase dataBase;
    /**
     * 根据mapid获取图标数据
     * @param mapid 图标id
     */
    @Override
    public Map<String,Object> getImageByMapId(@PathVariable String mapid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("sysmapids", mapid);
        params.put("select_image", true);
        Map<String, Object> result = zabbix.call("image.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> imageList = new HashMap<>();
        Map<String, ImageExtend> allSysIconsMap = this.getSysIcons();
        for (Map<String, Object> item: mapList) {
            imageList.put((String) item.get("imageid"), allSysIconsMap.get(item.get("imageid")));
        }
        return imageList;
    }

    /**
     * 获取所有系统图标
     */
    @Override
    public Map<String, ImageExtend> getSysIcons() {

//        Zabbix zabbix = Zabbix.getZabbix();
//        Map<String,Object> params = new HashMap<String,Object>();
//        params.put("output", "extend");
//        params.put("select_image", true);
//        Map<String, Object> result = zabbix.call("image.get", params);
//        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");

        List<ImageExtend> imageExtends = this.listAll();
        Map<String, ImageExtend> imageMap = new HashMap<>();
        for (ImageExtend item: imageExtends) {
            imageMap.put(item.getImageid(), item);
        }
        return imageMap;
    }

    /**
     * 获取hostid和image的关系
     */
    @Override
    public Map<String, SysImage> getImageAndHostid() {
        String sql = "select ht.hostid, m.imageid from images m, t_dist_template_icons tns, hosts_templates ht " +
                " where m.imageid=tns.icons_id and tns.template_id=ht.templateid and " +
                " tns.delete_flag=0";
        List<SysImage> sysImages = this.dataBase.query4Model(sql, SysImage.class);

        Map<String, SysImage> sysImageMap = new HashMap<>();
        for (SysImage item: sysImages) {
            sysImageMap.put(item.getHostid(), item);
        }
        return sysImageMap;
    }

    /**
     * 分页获取获取所有图标数据
     */
    @Override
    public PageResult<ImageExtend> pageQuerySysMap(@PathVariable long pageNo, @PathVariable int pageSize) {
        String sql = "select s.imageid, s.imagetype, s.name, s.image, s.imageformat from images s where s.imageid>187";
        return this.dataBase.query4Model(sql, pageNo, pageSize, ImageExtend.class);
    }

    /**
     * sql获取所有图标数据
     */
    @Override
    public List<ImageExtend> listAll() {
        String sql = "select s.imageid, s.imagetype, s.name, s.image, s.imageformat from images s";
        return this.dataBase.query4Model(sql, ImageExtend.class);
    }

    /**
     * 删除图标
     * @param imageid 图标id
     */
    @Override
    public Map<String, Object> deleteImage(@PathVariable String imageid) {
        Zabbix zabbix = Zabbix.getZabbix();
        List params = new ArrayList();
        params.add(imageid);
        Map<String, Object> result = zabbix.call("image.delete", params);
        return result;
    }
}
